/*
 * Log4RedManOdd.java
 *
 * Created on July 24, 2002, 6:32 PM
 */

package redman;

import org.apache.log4j.*;
import java.io.*;
import java.util.*;
import java.net.*;

/**
 *
 * @author  llaranje
 * @version 
 */
public class Log4RedManOdd {
    
   private static final Category trace = Category.getInstance( Log4RedManOdd.class );
   private static final String PROPERTIES_FILE = "log4redmanOdd.properties";
   private static final String TRACE_FILE = "redmanOdd";
   private static String redmandir = ".";
   
   //--------------------------------------------------------------------------

   /**
    * Static initializer. Initialization function is performed here to ensure
    * log4j is initialized before any other callMain functions are called.
    */
   static
   {
       if (RedMan.cfgfiledir != null) redmandir = RedMan.cfgfiledir;
      //
      // Load the configuration properties
      //
      File propFile = new File( redmandir, PROPERTIES_FILE );
      Properties props = new Properties();
      FileInputStream is = null;
      
      System.err.println( "Log4JService: Using properties file '" + redmandir + "/" + PROPERTIES_FILE 
                                + "', and trace file 'logs/" + TRACE_FILE + "'" );

      if( propFile.exists() == false )
      {
         System.err.println( "Log4RedMan: Properties file doesn't exist, will exit" );
      }
      else
      {
         try
         {
            is = new FileInputStream( propFile );
            props.load( is );

            // set the appender file location
            String localhost = InetAddress.getLocalHost().getHostName();
            String filename = redmandir + "/../logs/" + TRACE_FILE + "-" + localhost + ".log";
            // String filename = "/tmp/" + TRACE_FILE + "-" + localhost + ".log"; 
            props.setProperty( "log4j.appender.redmanlogger.File", filename );

            //
            // Configure log4j
            //
            PropertyConfigurator.configure( props );

            trace.info("Log configuration loaded from " + propFile.getAbsolutePath());
         }
         catch( Exception x )
         {
            System.err.println( "Log4RedManOdd: Unexpected exception while loading properties, " + x + ", exiting" );
         }
      }
   }

    /** Creates new Log4RedMan */
    public Log4RedManOdd() {
    }

}